import grace from "../../utils/grace.js"
grace.page({
      data: {
            carts: {
                  list: [],
                  total: 0
            }, //购物车列表
            cart_vals: '', //选择的商品编号
            is_special: 0,
            delivery_time_selected: 0,
            delivery_time_array: ['上午10:00-12:00', '下午4:00-6:00'],
            message: '', //留言
            defaultAddress: {
                  address_id: 0
            },
            amount: {
                  total_amount: '￥0',
                  shipping_fee: '￥0',
                  order_amount: '￥0',
                  activity_notice: ''
            },
            coupon_id: 0,
            delivery_mode_arr: ['配送到家', '门店自提'],
            delivery_mode: 0, //送货方式 1配送到家 2门店自提
            self_lift_store_id: 0, //门店自提ID
            self_lift_store_name: '', //门店自提,
            isSubmit: false,
            isFixed: false
      },
      onLoad: function(options) {
            let params = options[0][0];
            if (params && params['cart_vals']) {
                  this.$data.cart_vals = params['cart_vals'];
                  this.$data.is_special = 0;
            }
            this.loadData();
            this.loadAddress();
            this.calcTotal();

            this.$bus.$on("update_addres_event", (data) => {
                  this.loadAddress();
            });
            this.$bus.$on("update_phone_event", (data) => {
                  this.$data.amount.is_auth = true;
            });
            this.dialog = this.selectComponent("#dialog_phone");
      },
      $onBackData(data) {
            if (data) {
                  if (data['coupon_id']) {
                        this.$data.coupon_id = data['coupon_id'];
                        this.calcTotal();
                  } else if (data['store']) {
                        let store = data['store'];
                        this.$data.self_lift_store_id = store['store_self_id']; //门店自提ID
                        this.$data.self_lift_store_name = store['store_name']; //门店自提
                        this.calcTotal();
                  }
            }
      },
      messagefocus: function() {
            this.$data.isFixed = false;
      },
      messageblur: function() {
            this.$data.isFixed = true;
      },
      loadData: function() {
            this.$http.get('api/cart/list', {
                  cart_vals: this.$data.cart_vals,
                  is_special: this.$data.is_special
            }).then((res) => {
                  this.$data.carts = res;
            });
      },
      calcTotal: function() {
            let coupon_id = this.$data.coupon_id;
            let cart_vals = this.$data.cart_vals;
            let delivery_mode = parseInt(this.$data.delivery_mode) + 1;
            this.$http.get('api/cart/statistics', {
                  delivery_mode: delivery_mode,
                  coupon_id: coupon_id,
                  cart_vals: cart_vals
            }).then((res) => {
                  this.$data.amount = res;
            })
      },
      //
      loadStore: function() {
            this.$http.get('api/home/store_self_list', {}).then((res) => {
                  this.$data.store_list = res.list;
            });
      },
      //加载地址
      loadAddress: function() {
            this.$http.get('api/user_address/list', {}).then((res) => {
                  if (res.list.length > 0) {
                        this.$data.defaultAddress = res.list[0];
                  }
            });
      },
      chooseCoupon: function(e) {
            wx.navigateTo({
                  url: '../my_ticket/my_ticket?isChoose=1',
            });
      },
      submitOrder: function(e) {
            // if (this.$data.cart_vals.length == 0){
            //   this.toast('请先选择商品!');
            //   return;
            // }
            if (this.$data.isSubmit) {
                  return;
            }
            this.$data.isSubmit = true;
            wx.showToast({
                  title: '提交中..',
                  icon: 'loading',
                  duration: 2000
            })
            let coupon_id = this.$data.coupon_id;
            let that = this;
            let delivery_time = this.$data.delivery_time_array[this.$data.delivery_time_selected];
            let delivery_mode = parseInt(this.$data.delivery_mode) + 1;
            let params = {
                  cart_vals: this.$data.cart_vals,
                  coupon_id: coupon_id,
                  delivery_time: delivery_time,
                  address_id: this.$data.defaultAddress.address_id,
                  message: this.$data.message,
                  delivery_mode: delivery_mode,
                  store_self_id: this.$data.self_lift_store_id,
                  is_special: this.$data.is_special
            };

            this.$http.post('api/order/user_submit_order', params).then((res) => {
                  grace.bus.$emit("update_cart_event");
                  let order_amount = this.$data.amount.order_amount;
                  wx.redirectTo({
                        url: '../pay/pay?order_id=' + res.order_id + '&order_amount=' + order_amount
                  })
            }).catch(err => {
                  this.$data.isSubmit = false;
            })
      },
      bindPickerChange: function(e) {
            this.$data.delivery_time_selected = e.detail.value;
      },
      bindStroreChange: function(e) {
            this.$data.self_lift_store_id = 0; //门店自提ID
            this.$data.self_lift_store_name = ''; //门店自提
            this.$data.delivery_mode = e.detail.value;
            if (e.detail.value == 1) {
                  wx.navigateTo({
                        url: '../order_taker_store/order_taker_store',
                  })
            } else {
                  this.calcTotal();
            }
      },
      messageInput: function(e) {
            this.$data.message = e.detail.value;
      },
      openOrder: function() {
            wx.navigateTo({
                  url: '../my_order/my_order',
            })
      },
      testPay: function() {
            let that = this;
            this.$http.get('api/order/user_order_pay', {
                  order_id: 1
            }).then((res) => {
                  let data = JSON.parse(res.data);
                  wx.requestPayment({
                        'timeStamp': data.timeStamp,
                        'nonceStr': data.nonceStr,
                        'package': data.package,
                        'signType': data.signType,
                        'paySign': data.paySign,
                        'success': function(res) {
                              that.toast('支付成功');
                              wx.redirectTo({
                                    url: '../my_order/my_order',
                              });
                        },
                        'fail': function(res) {
                              that.toast('pay fail', +JSON.stringify(res));
                        }
                  })
            })
      },

      showDialog() {
            this.dialog.showDialog();
      },
      hideDialog() {
            this.dialog.hideDialog();
      }
})